package com.jstyle.jclife.view;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.WeekView;

/**
 * 多彩周视图
 * Created by huanghaibin on 2017/11/29.
 */

public class SleepWeekView extends WeekView {

    private int mRadius;
    private Paint mRingPaint = new Paint();
    private int mRingRadius;

    public SleepWeekView(Context context) {
        super(context);
        //兼容硬件加速无效的代码
        setLayerType(View.LAYER_TYPE_SOFTWARE, mSelectedPaint);
        //4.0以上硬件加速会导致无效
        mSelectedPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));

        setLayerType(View.LAYER_TYPE_SOFTWARE, mSchemePaint);
        mSchemePaint.setStyle(Paint.Style.STROKE);
        mSchemePaint.setStrokeWidth(dipToPx(context, 1));
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(Color.parseColor("#012e3d"));
        mRingPaint.setStyle(Paint.Style.FILL);
        mRingPaint.setStrokeWidth(dipToPx(context, 2));
        setLayerType(View.LAYER_TYPE_SOFTWARE, mRingPaint);
     //   mRingPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 6 * 2;
        mRingRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
        mSelectTextPaint.setTextSize(dipToPx(getContext(),17));
    }

    /**
     * 如果需要点击Scheme没有效果，则return true
     *
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return false 则不绘制onDrawScheme，因为这里背景色是互斥的
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
//        int cx = x + mItemWidth / 2;
//        int cy = mItemHeight / 2;
//        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
//        canvas.drawCircle(cx, cy, mRingRadius, mRingPaint);
        return true;
    }


    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x) {
        int cx = x + mItemWidth / 2;
        int cy =  mItemHeight / 2;


        Rect rect=new Rect();
        rect.left=cx-mItemWidth/3;
        rect.right=cx+mItemWidth/3;
        rect.top=cy-mItemHeight/3;
        rect.bottom=cy+mItemHeight/3;
        canvas.drawRect(rect,mSchemePaint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine - dipToPx(getContext(), 1);
        int cx = x + mItemWidth / 2;

        int cy =  mItemHeight / 2;
        Rect rect=new Rect();
        rect.left=cx-mItemWidth/3;
        rect.right=cx+mItemWidth/3;
        rect.top=cy-mItemHeight/3;
        rect.bottom=cy+mItemHeight/3;
        canvas.drawRect(rect,mRingPaint);
         if (hasScheme) {
             mSchemeTextPaint.setColor(Color.WHITE);

             canvas.drawText( String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
             mCurMonthTextPaint.setColor(Color.WHITE);

             canvas.drawText( String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }


    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    @SuppressWarnings("all")
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
